import clientEnum from "./client.enum"
import clientFunc from "./client.func"

export default class BoxSource {
  id: any
  updateSourceCall: any
  userData: any
  volumeCache: {
    center: { x: number; y: number; z: number }
    size: { x: number; y: number; z: number }
    orientation: { yaw: number; pitch: number; roll: number }
  }
  constructor(t, e, s, i) {
    this.id = t
    this.updateSourceCall = e
    this.userData = s
    this.volumeCache = {
      center: { x: 0, y: 0, z: 0 },
      size: { x: 1 / 0, y: 1 / 0, z: 1 / 0 },
      orientation: { yaw: 0, pitch: 0, roll: 0 }
    }
    this.updateCache(i.center, i.size, i.orientation)
  }
  get facade() {
    const t = {
      center: Object.assign({}, this.volumeCache.center),
      size: Object.assign({}, this.volumeCache.size),
      orientation: Object.assign({}, this.volumeCache.orientation)
    }
    const e = this,
      s = new (class BoxFacade {
        get type() {
          return clientEnum.ct.BOX
        }
        get volume() {
          return t
        }
        get userData() {
          return e.userData
        }
        commit() {
          e.commit(this)
        }
      })()
    return clientEnum.ot.map.set(s, this), s
  }
  commit(t) {
    this.hasSourceChanged(t) &&
      (this.updateCache(t.volume.center, t.volume.size, t.volume.orientation), this.updateSourceCall(this.id, clientEnum.ct.BOX, this.volumeCache))
  }
  hasSourceChanged(t) {
    return !(
      this.volumeCache.center.x === t.volume.center.x &&
      this.volumeCache.center.y === t.volume.center.y &&
      this.volumeCache.center.z === t.volume.center.z &&
      this.volumeCache.size.x === t.volume.size.x &&
      this.volumeCache.size.y === t.volume.size.y &&
      this.volumeCache.size.z === t.volume.size.z &&
      this.volumeCache.orientation.yaw === t.volume.orientation.yaw &&
      this.volumeCache.orientation.pitch === t.volume.orientation.pitch &&
      this.volumeCache.orientation.roll === t.volume.orientation.roll
    )
  }
  updateCache(t, e, s) {
    clientFunc.P(this.volumeCache.center, t),
      clientFunc.P(this.volumeCache.size, e),
      (function (t, e, ...s) {
        for (const i of s) t[i] = e[i]
      })(this.volumeCache.orientation, s, "yaw", "pitch", "roll")
  }
}
